var salt = sessionStorage.getItem('usersalt');
var from = getQueryVariable("test");
var edit = getQueryVariable("edit");
var nobtn = getQueryVariable("nobtn");

if (from) {
    $(".btn_box").remove();
}
if(nobtn){
  $(".choice_box").remove();
}
var choiceArr = sessionStorage.getItem("choiceArr") ? sessionStorage.getItem("choiceArr").split(",") : [];
var typeArr = sessionStorage.getItem("typeArr") ? sessionStorage.getItem("typeArr").split(",") : [0, 0, 0];

var app = new Vue({
    el: ".layui-form",
    data: {
        list: [],
        baseurl:url,
        page:"",
        count:'',
    },
    beforeCreate: function () {

    },
    updated: function () {
        initChoice(choiceArr);
        layui.use(['form'], function () {
            var form = layui.form
            form.render();
        })
    }
})

layui.use(['form', 'layer', 'laypage'], function () {
    var form = layui.form, laypage = layui.laypage,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    if (!sessionStorage.getItem('username')) {
        layer.msg('请先登录');
        setTimeout("top.location.href='../../../index.html'", 1000);
    }

    getData();
    $.post(url + 'question/getTypeName', {Token: salt}, function (data) {
        if (data.code == 0) {
            var list = data.data;
            var opt = '<option value="">请选择</option>'
            for (var i = 0; i < list.length; i++) {
                opt += '<option value="' + list[i].id + '" >' + list[i].name + '</option>'
            }
            $('#typeName').append(opt);
            form.render();
        } else {
            alert(data.msg);
        }
    })
    $.post(url + 'question/getDifferServ', {Token: salt}, function (data) {
        if (data.code == 0) {
            var list = data.data;
            var opt = '<option value="">请选择</option>'
            for (var i = 0; i < list.length; i++) {
                opt += '<option value="' + list[i].id + '" >' + list[i].name + '</option>'
            }
            $('#differServ').append(opt);
            form.render();
        } else {
            alert(data.msg);
        }
    });
    form.on('checkbox', function (obj) {
        var data = $(obj.elem);
        var id = data.attr("id");
        var questiontype = data.attr("questiontype");
        if (!id) {
            var checkBox = data.parents(".subject_box").find(".subject input[type='checkbox']");
            checkBox.prop("checked", data.prop("checked"));
            checkBox.each(function () {
                if (data.prop("checked")) {
                    if (choiceArr.indexOf($(this).attr('id')) == -1) {
                        choiceArr.push($(this).attr('id'));
                        typeArr[$(this).attr('questiontype')]++
                    }
                } else {
                    choiceArr.remove($(this).attr('id'));
                    typeArr[$(this).attr('questiontype')]--
                }
            })
            form.render('checkbox');
        } else {
            data.prop("checked") ? choiceArr.push(id) : choiceArr.remove(id);
            data.prop("checked") ? typeArr[questiontype]++ : typeArr[questiontype]--
        }
        $(".choice_box .choice_num span").text(choiceArr.length);
        console.log(choiceArr);
    });
  $(".import_btn").click(function() {
    layer.open({
      type: 2,
      title: false,
      content: "page/import.html?url=question",
      area: ['700px', '300px'],
      cancel: function(index, layero){ 
				getData();
		}
    });
  });

});

function getData(page) {
    var sendData = {
        limit: 10,
        page: page ? page : 1,
        differServ: $("#differServ").val(),
        typeName: $("#typeName").val(),
        questionType: $("#questionType").val(),
        title: $("#title").val(),
        Token: salt
    }

    // 获取数据
    $.post(url + 'question/getQuestionPage', sendData, function (data) {
        if (data.code == 0) {
            if(!page){
                layui.use(['laypage'], function () {
                    var laypage = layui.laypage;
                    laypage.render({
                        elem: 'demo0'
                        , count: data.count,
                        jump: function (obj, first) {
                            if (!first) {
                                getData(obj.curr)
                            }
                        }
                    });
                })
            }
            app.count = data.count;
            app.page = sendData.page;
            var list = data.data;
            list.forEach(function (item) {
              if(item.fileId){
                item.fileId = item.fileId.split(",");
              }
              if(item.parseFileId){
                item.parseFileId = item.parseFileId.split(",");
              }
                item.options = JSON.parse(item.options);
                item.options.forEach(function (it) {
                    if(it.fileId){
                        it.fileId = it.fileId.split(",");
                        console.log(it.fileId)
                    }
                })
            })
            app.list = list;
            console.log(app.list)
        } else {
            alert(data.msg);
        }
    })
}


$(".add_btn").click(function () {
    location.href = "../add/add.html"
})

$(".edit_btn").click(function () {
    var arr = [];
    $(".subject input[type=checkbox]").each(function () {
        if ($(this).prop('checked')) {
            arr.push($(this).attr("id"));
        }

    })
    if (arr.length > 1) {
        alert("只能编辑一个题目");
        return
    }
    if (arr.length == 0) {
        alert("请选择一个题目");
        return
    }
    location.href = "../edit/edit.html?id=" + arr[0];
})

$("body").on("click", "#seleCheck,#findscri", function () {
    getData()
});


$("body").on("click", ".del_btn", function () {
    var arr = [];
    $(".subject input[type=checkbox]").each(function () {
        if ($(this).prop('checked')) {
            arr.push($(this).attr("id"));
            // arr=$(this).attr("id")
        }

    })
    if (arr.length > 0) {
        $.post(url + 'question/delQuestion', {id: arr.toString(), Token: salt}, function (data) {
            if (data.code == 0) {
                layer.alert("删除成功", {
                    btn: ['确定'] //按钮
                }, function (index) {
                    layer.close(index);
                    getData();
                });
            } else {
                alert(data.msg);
            }
            console.log(app.list);
        })
    }

})

$("body").on("click", ".choice_box .sure", function () {
    console.log(choiceArr)
    sessionStorage.setItem("choiceArr", choiceArr);
    sessionStorage.setItem("typeArr", typeArr);
    if(edit){
        sessionStorage.setItem("edit", edit);
    }
    history.go(-1);
});

function initChoice(choiceArr) {
    console.log(choiceArr)
    if (from && sessionStorage.getItem("choiceArr")) {
        var choiceArr = sessionStorage.getItem("choiceArr").split(",");
        $(".choice_box .choice_num span").text(choiceArr.length);
        $(".subject input[type='checkbox']").each(function () {
            if (choiceArr.indexOf($(this).attr("id")) != -1) {
                $(this).prop("checked", true);
            }else{
                $(this).prop("checked", false);
            }
        })
    }else{
        if(!choiceArr){
            return
        }
        $(".subject input[type='checkbox']").each(function () {
            if (choiceArr.indexOf($(this).attr("id")) != -1) {
                $(this).prop("checked", true);
            }else{
                $(this).prop("checked", false);
            }
        })
        $(".subject input[type='checkbox']:checked").length == 10?$(".allcheck input[type='checkbox']").prop("checked", true):$(".allcheck input[type='checkbox']").prop("checked", false)
    }
}




